﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ServiceModel;
using BookClubIt.Processor;
using DTO = BookClubIt.DTO;

namespace BookClubIt
{
    public partial class Books : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
          //  Page.Title = "Search for Books";
            if (!IsPostBack)
            {
                BindData();
            }

        }

        private void BindData()
        {
            string query = null;
            if (Request.QueryString["query"] != null)
            {
                query = Request.QueryString["query"];


                lnkBookTab.NavigateUrl = "search.aspx?tab=books&query=" + query;
                lnkBookClubsTab.NavigateUrl = "search.aspx?tab=clubs&query=" + query;
                if (Page.Request.QueryString["tab"] == "books")
                {
                    pnlBookClubSearch.Visible = false;
                    lblBookSearchTitle.Text = query;
                    lnkBookTab.Attributes.Add("class", "at");
                    DTO.Books books = AmazonProcessorInstance.SearchBookByTitle(query, "relevancerank");
                    if (books.Count > 0)
                    {
                        rptBooks.DataSource = books;
                        rptBooks.DataBind();
                    }
                    else
                    {
                        pnlNoBooks.Visible = true;
                    }
                }
                else if (Page.Request.QueryString["tab"] == "clubs")
                {
                    pnlBookSearch.Visible = false;
                    lblClubQuery.Visible = true;
                    lblClubQuery.Text = query;
                    lnkBookClubsTab.Attributes.Add("class", "at");
                    DTO.BookClubs clubs = BookClubProcessorInstance.SearchBookClubsByTitle(query);
                    if (clubs.Count > 0)
                    {
                        grdBrowseClubs.DataSource = clubs;
                        grdBrowseClubs.DataBind();
                    }
                    else
                    {
                        pnlNoClubs.Visible = true;
                    }

                }
                else
                {
                    pnlBookSearch.Visible = false;
                    pnlBookClubSearch.Visible = false;
                }
            }
            else
            {
                pnlBookSearch.Visible = false;
                pnlBookClubSearch.Visible = false;

            }
        }
        public void BindData(DTO.BookClubs clubs)
        {
            grdBrowseClubs.DataSource = clubs;
            grdBrowseClubs.DataBind();
        }
        protected void SearchBook_Click(object sender, EventArgs e)
        {
            string tab = Request.QueryString["tab"];
            string url = HttpContext.Current.Request.Url.AbsolutePath;
            url = url + "?tab=" + tab + "&query=" + txtSearch.Text;
            Response.Redirect(url);


        }
        protected void grdBrowseClubs_OnRowDataBound(object sender, GridViewRowEventArgs e)
        {


            DTO.BookClub club = e.Row.DataItem as DTO.BookClub;

            if (e.Row.RowType != DataControlRowType.DataRow)
                return;

            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Image imgClubAvatar = ((Image)e.Row.FindControl("imgClubAvatar"));
                Image imgCurrentBook = ((Image)e.Row.FindControl("imgCurrentBook"));
                HyperLink lnkBookClub = ((HyperLink)e.Row.FindControl("lnkBookClub"));
                HyperLink lnkViewClub = ((HyperLink)e.Row.FindControl("lnkViewClub"));
                Label lblClubType = ((Label)e.Row.FindControl("lblClubType"));
                Label lblNumMembers = ((Label)e.Row.FindControl("lblNumMembers"));
                Label lblCurrentBook = ((Label)e.Row.FindControl("lblCurrentBook"));
                Label lblCreatedDate = ((Label)e.Row.FindControl("lblCreatedDate"));
                Label lblGenreType = ((Label)e.Row.FindControl("lblGenreType"));

                int clubTypeInt = club.ClubType;
                int genreTypeInt = club.GenreType;
                int clubId = club.Id;
                //    string currentASIN = club.CurrentBook;

                DTO.ClubType type = BookClubProcessorInstance.GetBookClubType(clubTypeInt);
                DTO.GenreType gtype = BookClubProcessorInstance.GetBookClubGenre(genreTypeInt);
                DTO.Members members = MemberProcessorInstance.GetBookClubMembersById(clubId);
                DTO.Book current_book = BookProcessorInstance.GetCurrentBook(clubId);
                //    DTO.Book book = BookProcessorInstance.GetBookByASIN();

                imgClubAvatar.ImageUrl = "avatars/club/" + club.AvatarURL;
                lnkBookClub.Text = club.Name;
                lnkBookClub.NavigateUrl = "bookclub/default.aspx?id=" + club.Id;
                lnkViewClub.NavigateUrl = "bookclub/default.aspx?id=" + club.Id;

                lblCurrentBook.Text = current_book.Title;
                imgCurrentBook.ImageUrl = current_book.SmallImageURL;
                lblCreatedDate.Text = club.CreatedDate.ToString("MMM dd, yyyy");
                lblGenreType.Text = gtype.GenreName;
                if (type.ClubTypeName == "Public")
                    lblClubType.CssClass = "status public";

                if (type.ClubTypeName == "Private")
                    lblClubType.CssClass = "status private";

                lblClubType.Text = type.ClubTypeName;

                lblNumMembers.Text = members.Count.ToString();

            }

        }
        protected void grdBrowseClubs_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            string query = null;
            if (Request.QueryString["query"] != null)
            query = Request.QueryString["query"];

            grdBrowseClubs.PageIndex = e.NewPageIndex;
            DTO.BookClubs clubs = BookClubProcessorInstance.SearchBookClubsByTitle(query);
            grdBrowseClubs.DataSource = clubs;
            grdBrowseClubs.DataBind();
        }
        protected void rptBooks_OnItemDataBound(object sender, RepeaterItemEventArgs e)
        {

            RepeaterItem item = e.Item; if (item.ItemType == ListItemType.AlternatingItem || item.ItemType == ListItemType.Item)
            {
                DTO.Book book = e.Item.DataItem as DTO.Book;

                HyperLink lnkBookLink = (HyperLink)item.FindControl("lnkBookLink");
                Label lblAuthor = (Label)item.FindControl("lblAuthor");
                Image imgBookImage = (Image)item.FindControl("imgBookImage");

                lnkBookLink.Text = book.Title;
                

                imgBookImage.ImageUrl = book.SmallImageURL;
                lblAuthor.Text = "" + book.Author;

                // if book is already in database, get the id and add it to book link
                if (BookProcessorInstance.haveBookInDb(book.ASIN))
                {
                    DTO.Book old_book = BookProcessorInstance.GetBookByASIN(book.ASIN);
                    lnkBookLink.NavigateUrl = "book.aspx?id=" + old_book.Id;
                }
                else
                {
                    DTO.Book new_book = BookProcessorInstance.SaveBook(book);
                    lnkBookLink.NavigateUrl = "book.aspx?id=" + new_book.Id;
                }
            }

        }
    }
}